package com.leyou.item.web;

import com.leyou.item.dto.SpecGroupDTO;
import com.leyou.item.dto.SpecParamDTO;
import com.leyou.item.entity.SpecGroup;
import com.leyou.item.entity.SpecParam;
import com.leyou.item.service.SpecGroupService;
import com.leyou.item.service.SpecParamService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 虎哥
 */
@RestController
@RequestMapping("spec")
public class SpecController {

    private SpecGroupService groupService;
    private SpecParamService paramService;

    public SpecController(SpecGroupService groupService, SpecParamService paramService) {
        this.groupService = groupService;
        this.paramService = paramService;
    }



    /**
     * 查询规格参数
     * @param categoryId  分类Id
     * @param groupId  规格组Id
     * @param searching  是否搜索
     * @return  参数集合
     */
    @GetMapping("/params")
    public ResponseEntity<List<SpecParamDTO>> querySpecParams(
            @RequestParam(value = "categoryId",required = false) Long categoryId,
            @RequestParam(value = "groupId",required = false) Long groupId,
            @RequestParam(value = "searching",required = false) Boolean searching
    ){
        //select * from tb_spec_param p where p.category_id = 76 and p.group_id = 1 and searching = 0;

        List<SpecParamDTO> list = paramService.querySpecParams(categoryId,groupId,searching);
        return ResponseEntity.ok(list);
    }

    /**
     * 根据分类查询规格组
     * @param categoryId  分类Id
     * @return  规格组集合
     */
    @GetMapping("/groups/of/category")
    public ResponseEntity<List<SpecGroupDTO>> queryGroupByCategoryId(@RequestParam("id") Long categoryId){
        //查询
        List<SpecGroup> list = groupService.query().eq("category_id", categoryId).list();
        //转换
        List<SpecGroupDTO> dtoList = list.stream().map(SpecGroupDTO::new).collect(Collectors.toList());
        //返回
        return ResponseEntity.ok(dtoList);
    }

    @GetMapping("/list")
    public ResponseEntity<List<SpecGroupDTO>> querySpecList(@RequestParam("id") Long categoryId){
        List<SpecGroupDTO> list =  groupService.querySpecList(categoryId);
        return ResponseEntity.ok(list);
    }

    /**
     * 新增规格组
     * @return 无
     */
    @PostMapping("/group")
    public ResponseEntity<Void> savegroup(@RequestBody SpecGroupDTO groupDTO){
        //新增规格组
        groupService.save(groupDTO.toEntity(SpecGroup.class));
        //返回集合
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 修改规格组
     * @param groupDTO  规格组信息
     * @return  无
     */
    @PutMapping("/group")
    public ResponseEntity<Void> updateGroup(@RequestBody SpecGroupDTO groupDTO){
        //修改规格组
        groupService.updateById(groupDTO.toEntity(SpecGroup.class));
       //返回结果
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    /**
     * 新增规格参数
     * @param paramDTO  规格参数
     * @return 返回无
     */
    @PostMapping("/param")
    public ResponseEntity<Void> saveParams(@RequestBody SpecParamDTO paramDTO){
        //新增规格参数
        paramService.save(paramDTO.toEntity(SpecParam.class));
        //返回结果
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 更新规格参数
     * @param paramDTO  规格参数
     * @return  返回无
     */
    @PutMapping("/param")
    public ResponseEntity<Void> updateParam(@RequestBody SpecParamDTO paramDTO){
        //更新规格参数
        paramService.updateById(paramDTO.toEntity(SpecParam.class));
        //返回
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }



}
